<template>


    <a-modal :title="title" :width="600" :destroyOnClose="true" :maskClosable="false"
             :visible="visible" @cancel="handleCancel"
>

            <a-checkbox-group @change="onChange"  :value="value"  >
                <a-row :gutter="[16,16]">
                    <a-col :span="8"   v-for="(item,index) in  groupOPT" :key="index">
                        <a-checkbox :value="item.value"> {{item.label}}</a-checkbox>
                    </a-col>
                </a-row>
            </a-checkbox-group>


        <!-- Modal 页脚 -->
        <template slot="footer">
            <a-button   @click="handleCancel">关闭</a-button>
            <a-button type="primary" :loading="loading" @click="onSubmit">保存</a-button>
        </template>
        </a-modal>

</template>
<script>
    import {groupDict} from '../../../util/internal.api';
    import {saveStaffGroup,getStaffGroup} from "../public/http";

    export default {
        props:["staffObj"],
        data() {
            return {
                loading: false, //按钮Loading
                title:"分组设置",

                visible: true, //modal 显示状态
                groupOPT:[],
                value:[], //多选选中的项
            };
        },
        methods: {


            async init() {
                //职务字典获取
                await  groupDict().then(res=>this.groupOPT=res );
                //获取人员标签
                await  getStaffGroup(this.staffObj.code).then(res=>this.value=res.data.result);

            },


            //关闭弹窗
            handleCancel() {
                this.visible = false;
                this.$emit('closeModal', {
                    render: false
                });
            },




            //完成监听
            async  onSubmit(e) {
                //AJAX时间数据处理
                let data={dataArr :this.value.toString() };
               await saveStaffGroup(this.staffObj.code,data ).then(res=>{
                   if(res.code===0){
                       this.$notification.success({
                           message: '操作成功',
                           description: res.message
                       });

                       //关闭弹窗
                       this.visible = false;
                       this.$emit('closeModal', {render: false});
                   }else{
                       this.$notification.error({
                           message: '操作失败',
                           description: res.message
                       });

                   }

                });


            },

            //获取选择结果
            onChange(checkedValues) {
                this.value = checkedValues;
            },

        },

        created() {
            this.init();
        }

    };
</script>